package me.zhengjie.tools.utils

import com.alipay.api.AlipayApiException
import com.alipay.api.internal.util.AlipaySignature
import jakarta.servlet.http.HttpServletRequest
import me.zhengjie.tools.domain.AlipayConfig

import org.springframework.stereotype.Component
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.HashMap



/**
 * 支付宝工具类
 * @author zhengjie
 * @date 2018/09/30 14:04:35
 */
@Component
class AlipayUtils {
    val orderCode: String
        /**
         * 生成订单号
         * @return String
         */
        get() {
            val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            val a = (Math.random() * 9000.0).toInt() + 1000
            println(a)
            val date = Date()
            val str = sdf.format(date)
            val split = str.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
            val s = split[0] + split[1] + split[2]
            val split1 = s.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
            val s1 = split1[0] + split1[1]
            val split2 = s1.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
            return split2[0] + split2[1] + split2[2] + a
        }

    /**
     * 校验签名
     * @param request HttpServletRequest
     * @param alipay 阿里云配置
     * @return boolean
     */
    fun rsaCheck(request: HttpServletRequest, alipay: AlipayConfig?): Boolean {

        // 获取支付宝POST过来反馈信息
        val params: MutableMap<String, String> = HashMap(1)
        val requestParams: Map<*, *> = request.parameterMap
        for (o in requestParams.keys) {
            val name = o as String
            val values = requestParams[name] as Array<String>?
            var valueStr = ""
            for (i in values!!.indices) {
                valueStr = if (i == values.size - 1) valueStr + values[i] else valueStr + values[i] + ","
            }
            params[name] = valueStr
        }
        return try {
            AlipaySignature.rsaCheckV1(
                params,
                alipay!!.publicKey,
                alipay!!.charset,
                alipay!!.signType
            )
        } catch (e: AlipayApiException) {
            false
        }
    }
}